<?php
namespace Home\Controller;
use Think\Controller;
class VideosController extends BaseController {
    public function videolist(){
	        $videos= M('videos');//实例化news对象
	        $videoarea=$_GET['videoarea'];
	 		// dump($lost->count())
	 		/*$res=$videos->where("videoarea='$videoarea'")->order("vid desc")->select();  
	 		$this->videos=$res;*/
        $qrcodeList= $videos->where("videoarea='$videoarea'")->select();
        $this->qrcodeList=$qrcodeList;
        //分页
        $qrcodenum=$videos->where("videoarea='$videoarea'")->count();
        $Page = new \Think\Page($qrcodenum,3);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
每页<b>3</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->pageNav=$Page->show();//在页面{pageNav}
        $this->videos=$videos->where("videoarea='$videoarea'")->order("vid desc")->limit($Page->firstRow,3)->select();
	 		$this->display();
	 	}
       public function detail(){
            $videos= M('videos');//实例化news对象
            $vid=$_GET['vid'];
            // dump($lost->count())
            $res=$videos->where("vid='$vid'")->limit(4)->select();  //查询未结的失物启事，同时获取所有置顶的失物启事 
            $this->videos=$res;
            // dump($res);
         
            
            $this->display();
        }
       function search(){
       	$videos= M('videos');
        $videoname=$_GET['videoname'];
        $res=$videos->where("videoname like '%$videoname%'")->select();   
        $this->videos=$res;
        $this->display();
}
	 function download(){
	 	$vid=$_GET['vid'];
	 	 $videos= M('videos');
	 	 $res=$videos->where("vid='$vid'")->getField('videoname');
	 	 dump($res);
	 	$file="__PUBLIC__/Uploads/address/$res.txt/";
	 	dump($file);
    $fileName = $name ? $name : pathinfo($file,PATHINFO_FILENAME);
    $filePath = realpath($file);
    dump($filePath);
    $fp = fopen($file,'rb');
    dump($fp);
    if(!$filePath || !$fp){
        header('HTTP/1.1 404 Not Found');
        echo "Error: 404 Not Found.(server file path error)<!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding -->";
        exit;
    }
    
    $fileName = $fileName .'.'. pathinfo($filePath,PATHINFO_EXTENSION);
    $encoded_filename = urlencode($fileName);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);
    
    header('HTTP/1.1 200 OK');
    header( "Pragma: public" );
    header( "Expires: 0" );
    header("Content-type: application/octet-stream");
    header("Content-Length: ".filesize($filePath));
    header("Accept-Ranges: bytes");
    header("Accept-Length: ".filesize($filePath));
    
    $ua = $_SERVER["HTTP_USER_AGENT"];
    if (preg_match("/MSIE/", $ua)) {
        header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
        header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
    } else {
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
    }
    
    // ob_end_clean(); <--有些情况可能需要调用此函数
    // 输出文件内容
    fpassthru($fp);
    exit;
}
    }